package com.syx.friend.Service.Impl;

import com.syx.common.Model.OSSResult;
import com.syx.common.Service.OSSService;
import com.syx.commoncore.Model.Result;
import com.syx.commoncore.enums.Resultcode;
import com.syx.friend.Service.IFileService;
import com.syx.security.Exception.ServiceException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
public class IFileServiceImpl implements IFileService {

    @Resource
    private OSSService ossService;

    @Value("${file.oss.downloadUrl}")
    private String downloadUrl;

    @Override
    public Result upload(MultipartFile file) {
        try {
            String name = ossService.uploadFile(file).getName();
            OSSResult ossResult=new OSSResult();
            ossResult.setName(downloadUrl+name);
            ossResult.setSuccess(true);
            System.out.println(ossResult);


            return Result.success(ossResult);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Resultcode.FAILED_FILE_UPLOAD);
        }
    }
}